package cn.juforg.gobang;

import android.util.Log;

/**
 * 
 * @author Juforg
 * 棋子类
 */
public class Chess {

	private int x ;
	private int y ;
	private int flag;
	
	public Chess(int x, int y) {
		super();
		this.x = x;
		this.y = y;
		this.flag=ChessBoard.chessArray[x][y];
	}

	public Chess() {
		super();
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	@Override
	public int hashCode() {
		return x + y ;
	}

	public boolean isSameType(Chess chess){
		if (null!=chess && ChessBoard.EMPTY != chess.getFlag() && this.flag%2==chess.getFlag()%2) {//自己棋的标志和比较棋子的标志取余后比较相同就是自己的棋了
			return true;
		}
		return false;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this==obj){
			return true;
		}
		Chess otherchess = (Chess) obj;
		if(x != otherchess.x)
			return false;
		if(y != otherchess.y)
			return false;
		return true;
	}

	@Override
	public String toString() {
		String s = "["+x+"]["+y+"]";
		return s;
	}
	
	public Chess nextChess(char direction,int orientation){
		Chess c=new Chess();
		switch (direction) {
		case Constants.HORIZONTAL:
			c.setX(this.x+orientation);
			c.setY(this.y);
			break;
		case Constants.VERTICAL:
			c.setX(this.x);
			c.setY(this.y+orientation);
			break;
		case Constants.SLANT:
			c.setX(this.x+orientation);
			c.setY(this.y+orientation);
			break;
		case Constants.BACKWARD_SLANT:
			c.setX(this.x+orientation);
			c.setY(this.y-orientation);
			break;
		default:
			c=this;
			break;
		}
		//max -1 之后才是坐标
		if (Constants.CHESSBAORD_TYPE-1<c.getX() || c.getX()<0 || Constants.CHESSBAORD_TYPE-1<c.getY() || c.getY()<0) {
			return null;
		}
		try {
			c.setFlag(ChessBoard.chessArray[c.getX()][c.getY()]);
		} catch (Exception e) {
			Log.i("chess:", c.getX()+","+c.getY()+","+Integer.valueOf(c.getFlag()));
		}
		return c;
	}
	
	public boolean isEmpty(){
		if (Constants.CHESSBAORD_TYPE-1<this.getX()||Constants.CHESSBAORD_TYPE-1<this.getY()) {
			return true;
		}
		return ChessBoard.chessArray[this.x][this.y]==ChessBoard.EMPTY||this.flag==ChessBoard.EMPTY;
	}
}
